﻿using System;
using System.Collections.Generic;
using System.Text;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace WorkFlowCoreDemo
{
    public  class HelloWorldWorkflow : IWorkflow<MyNameClass>
    {
        public string Id => "HelloWorld";
        public int Version => 1;

        public void Build(IWorkflowBuilder<MyNameClass> builder)
        {
            builder

                .StartWith<HelloWorld>().Input(step => step.Name, data => data.MyName)               
                .Parallel()
                    .Do(then =>
                        then.StartWith(context => { Console.WriteLine("分支一开始"); ExecutionResult.Next(); })
                            .Then(context => { Console.WriteLine("分支一结束"); ExecutionResult.Next(); }))
                    .Do(then =>
                        then.StartWith(context => { Console.WriteLine("分支二开始"); ExecutionResult.Next(); })
                            .Then(context => { Console.WriteLine("分支二结束"); ExecutionResult.Next(); }))
                    .Do(then =>
                        then.StartWith(context => { Console.WriteLine("分支三开始"); ExecutionResult.Next(); })
                            .Then(context => { Console.WriteLine("分支三结束"); ExecutionResult.Next(); }))
                .Join()//Parallel的结构是分支的开始是Parallel()，结束是Join()，每个分支在Do语句中表示
                .Then(context => { Console.WriteLine("Parallel结束"); ExecutionResult.Next(); })
                //使用ForEach执行并行流程，这些流程的执行过程相同，不同的只是输入的参数
                .ForEach(data => data.MyNames)
                .Do(x => x
                    .StartWith<HelloWorld>()
                        .Input(step => step.Name, (data, context) => context.Item as string)
                    .Then<GoodbyeWorld>()
                        .Input(step => step.Name, (data, context) => context.Item as string)
                )
                .Then(context => { Console.WriteLine("ForEach结束"); ExecutionResult.Next(); })
                .Activity("activity-1", (data) => data.MyName)
                    .Output(data => data.MyName, step => step.Result)
                .Then<GoodbyeWorld>()
                    .Input(step => step.Name, data => data.MyName)
                .While(data => data.MyName.Length < 3)
                    .Do(x => x
                        .StartWith(context => { Console.WriteLine("输入小于3个字符"); ExecutionResult.Next(); })
                        .Activity("activity-1", (data) => data.MyName)
                        .Output(data => data.MyName, step => step.Result))
                //如果需要并行执行多个不同的流程，需要使用Parallel
                //.Activity("activity-2", (data) => data.MyName)
                //    .Output(data => data.MyName, step => step.Result)
                //    .Then<HelloWorld>().Input(step => step.Name, data => data.MyName)
                .Then<GoodbyeWorld>()
                    .Input(step => step.Name, data => data.MyName);
                //.StartWith(context => ExecutionResult.Next())
                ////这个事件的输出是将事件接收的外部参数(step.EventData)传递给流程的MyName属性。
                //.WaitFor("MyEvent", (data, context) => context.Workflow.Id, data => DateTime.Now)
                //.Output(data => data.MyName, step => step.EventData)
                ////.Output(data => data["Name"], step => (string)step.EventData)
                //// .Input(step => step.Name, data => data.MyName) 就是将流程数据中的MyName传递给步骤中的Name(step.Name)
                //.Then<HelloWorld>().Input(step => step.Name, data => data.MyName)
                //.Then<GoodbyeWorld>().Input(step => step.Name, data => data.MyName);
        }
    }
}
